package com.aric.springbootmusic.config;

import com.aric.springbootmusic.filter.JwtFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.ProviderManager;
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

@Configuration
@EnableMethodSecurity
public class SecurityConfig {
    //配置认证失败处理器
    @Autowired
    private AuthenticationEntryPoint authenticationEntryPoint;

    //配置权限异常处理器
    @Autowired
    private AccessDeniedHandler accessDeniedHandler;

    //配置UserDetailsService
    @Autowired
    private UserDetailsService userDetailsService;

    //配置JWT过滤器，用于校验token
    @Autowired
    private JwtFilter jwtFilter;

    //配置加密方式
    @Bean
    public PasswordEncoder passwordEncoder(){
        return new BCryptPasswordEncoder();
    }

    //配置DaoAuthenticationProvider
    @Bean
    public DaoAuthenticationProvider daoAuthenticationProvider(PasswordEncoder passwordEncoder){
        DaoAuthenticationProvider authenticationProvider=new DaoAuthenticationProvider(passwordEncoder);
        authenticationProvider.setUserDetailsService(userDetailsService);
        return authenticationProvider;
    }

    //配置认证管理器
    @Bean
    public AuthenticationManager authenticationManager(DaoAuthenticationProvider daoAuthenticationProvider){
        return new ProviderManager(daoAuthenticationProvider);
    }

    //配置Spring Security过滤器链
    @Bean
    public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
        //关闭跨站攻击csrf防护
        http.csrf(csrf->csrf.disable());
        //配置授权请求
        http.authorizeHttpRequests(request->{
            request.requestMatchers("/user/login","/music/download",
                            "/music/removeMusic","/download/**",
                    "/music/selectShowMusic").permitAll()
                    .anyRequest().authenticated();
        });

        //添加jwtTokenValidatorFilter过滤器
        http.addFilterBefore(jwtFilter, UsernamePasswordAuthenticationFilter.class);

        //处理认证和授权异常
        http.exceptionHandling(exeConfig -> {
            exeConfig.authenticationEntryPoint(authenticationEntryPoint).accessDeniedHandler(accessDeniedHandler);
        });

        return http.build();
    }
}
